package service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import model.Trole;
import model.Tuser;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pageModel.DataGrid;
import pageModel.User;
import service.UserServiceI;
import util.MD5Util;
import dao.RoleDaoI;
import dao.UserDaoI;

@Service("userService")
public class UserServiceImpl implements UserServiceI{
	
	private UserDaoI userDao;
    private RoleDaoI roleDao;
    public UserDaoI getUserDao() {
    	return userDao;
    }
    @Autowired
    public void setUserDao(UserDaoI userDao) {
    	this.userDao = userDao;
    }

   public RoleDaoI getRoleDao() {
		return roleDao;
	}
   @Autowired
	public void setRoleDao(RoleDaoI roleDao) {
		this.roleDao = roleDao;
	}
public User save(User user){
	   Tuser u=new   Tuser();
	   BeanUtils.copyProperties(user,u,new String[]{"pwd"});
	   u.setId(UUID.randomUUID().toString());
	   u.setCreatedatetime(new Date()); 
	   u.setPwd(MD5Util.md5(user.getPwd()));
	   Trole tr=roleDao.get( Trole.class, "d3938142-29f9-4a68-9c37-6a3c06ec3b28");
	   u.setTrole(tr);
	   userDao.save(u);
	   BeanUtils.copyProperties(u, user);
	   return user;
   }
   public User log(User user){
	   Map<String,Object>params=new HashMap<String,Object>();
	   params.put("logname",user.getLogname());
	   params.put("pwd", MD5Util.md5(user.getPwd()));
	   Tuser u=userDao.get("from Tuser u where u.logname=:logname and u.pwd=:pwd",params);
	   if(u!=null){
	   return user;
   }
	   return user;
   }
@Override
public DataGrid datagrid(User user) {
	DataGrid dg=new DataGrid();
	String hql="from Tuser  t where 1=1";
	Map<String,Object> params=new HashMap<String,Object>();
	hql = addWhere(user, hql, params);
	String totalHql="select count(*) "+hql;
	if(user.getSort()!=null){
	hql+=" order by "+user.getSort()+" "+user.getOrder();
	}
	List< Tuser> su=userDao.find(hql,params,user.getPage(),user.getRows());
	List<User> l=new ArrayList<User>();
	if(su!=null&&su.size()>0){
		for(  Tuser t :su){
			User u=new User();
			BeanUtils.copyProperties(t, u);
			if(t.getTrole()!=null){
				u.setRolename(t.getTrole().getRolename());
				u.setRoleid(t.getTrole().getId());
			}
			l.add(u);
		}
	}
	dg.setTotal(userDao.count(totalHql,params));
	dg.setRows(l);
	return dg;
}
private String addWhere(User user, String hql, Map<String, Object> params) {
	if(user.getName()!=null&&!user.getName().trim().equals("")){
	hql+=" and t.name like :name";
	params.put("name", "%%"+user.getName()+"%%");
	}
	if(user.getLogname()!=null&&!user.getLogname().trim().equals("")){
		hql+=" and t.logname like :logname";
		params.put("logname", "%%"+user.getLogname()+"%%");
		}
	if (user.getCreatedatetimeStart() != null) {
		hql += " and t.createdatetime >= :createdatetimeStart";
		params.put("createdatetimeStart", user.getCreatedatetimeStart());
	}
	if (user.getCreatedatetimeEnd() != null) {
		hql += " and t.createdatetime <= :createdatetimeEnd";
		params.put("createdatetimeEnd", user.getCreatedatetimeEnd());
	}
	return hql;
}
@Override
public void remove(String ids) {
	/*for(String id: ids.split(",")){
		Syuser u=userDao.get(Syuser.class, id);
		if(u!=null){
			userDao.delete(u);
		}
	}
*/
	String [] nids=ids.split(",");
	String hql="delete Tuser  t where t.id in(";
	for(int i=0;i<nids.length;i++){
		if(i>0){
			hql+=",";
		}
		hql+="'"+nids[i]+"'";
	}
	hql+=")";
	userDao.executeHql(hql);
	}
@Override
public void edit(User user) {
	// TODO Auto-generated method stub
	 Tuser t=userDao.get( Tuser.class, user.getId());
     BeanUtils.copyProperties(user, t,new String[]{"id","pwd"});
     if(user.getRoleid()!=null){
    	   Trole tr=new Trole();
		   tr.setId(user.getRoleid());
		   t.setTrole(tr);
		   t.setUpdatedatetime(new Date());
     }
     BeanUtils.copyProperties(t, user,new String[]{"id","pwd"});
}
@Override
public User findByLogname(String logname) {
	String hql="from Tuser  t where t.logname=:logname";
	Map<String,Object> params=new HashMap<String,Object>();
	params.put("logname", logname);
	Tuser t = userDao.get(hql, params);
	User u = new User();
	BeanUtils.copyProperties(t, u);
	return u;
}
@Override
public void editPwd(User user) {
	if(user!=null&&user.getPwd()!=null&&!user.getPwd().trim().equals("")){
		Tuser u = userDao.get(Tuser.class,user.getId());
		/*if (u.getPwd().equalsIgnoreCase(MD5Util.md5(user.getOldPwd()))&&!user.getOldPwd().trim().equals(user.getPwd().trim())) {// 说明原密码输入正确,并且新设置的密码不与原密码相同
*/			u.setPwd(MD5Util.md5(user.getPwd()));
			u.setUpdatedatetime(new Date());
		}
	/*}*/	
} 
}
 